package com.blog.service.impl;

 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.blog.constants.SystemConstants;
 import com.blog.domain.DTO.AddLinkDto;
 import com.blog.domain.DTO.LinkListDto;
 import com.blog.domain.DTO.LinkStatusDto;
 import com.blog.domain.DTO.StatusDto;
 import com.blog.domain.ResponseResult;
 import com.blog.domain.entity.Link;
 import com.blog.domain.vo.LinkVo;
 import com.blog.domain.vo.PageVo;
 import com.blog.mapper.LinkMapper;
 import com.blog.service.LinkService;
 import com.blog.utils.BeanCopyUtils;
 import com.blog.utils.SecurityUtils;
 import org.springframework.stereotype.Service;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;

 import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2024-08-25 16:04:51
 */
 @Service("linkService")
public class LinkServiceImpl extends ServiceImpl <LinkMapper, Link> implements LinkService {

  /**查询友链
   *
   * @return
   */
  @Override
  public ResponseResult getAllLink() {
   //查询所有审核通过的
   LambdaQueryWrapper<Link> lambdaQueryWrapper = new LambdaQueryWrapper<>();
   lambdaQueryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
   List<Link> linkList = list(lambdaQueryWrapper);
   //转换vo
   List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);
   //封装返回
   return ResponseResult.okResult(linkVos);
  }
 /**友链分页查询
  *
  * @param pageNum
  * @param pageSize
  * @param linkListDto
  * @return
  */

 @Override
 public ResponseResult linkList(Integer pageNum, Integer pageSize,
                                LinkListDto linkListDto) {

  //分页条件
  Page<Link> page = new Page<>();
  page.setCurrent(pageNum);
  page.setSize(pageSize);
  //查询条件
  LambdaQueryWrapper<Link> wrapper = new LambdaQueryWrapper<>();
  wrapper.eq(StringUtils.hasText(linkListDto.getStatus()),
          Link::getStatus,linkListDto.getStatus());

  wrapper.like(StringUtils.hasText(linkListDto.getName()),
          Link::getName,linkListDto.getName());
  //分页
  page(page,wrapper);
  //封装返回
  List<Link> records = page.getRecords();
  List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(records, LinkVo.class);

  long total = page.getTotal();

  PageVo pageVo = PageVo.builder()
          .rows(linkVos)
          .total(total)
          .build();
  return ResponseResult.okResult(pageVo);
 }

 /**新增友链
  *
  * @param addLinkDto
  * @return
  */
 @Override
 public ResponseResult addLink(AddLinkDto addLinkDto) {
  Link link = BeanCopyUtils.copyBean(addLinkDto, Link.class);
  save(link);
  return ResponseResult.okResult();
 }

 /**根据id查询友链回显
  *
  * @param id
  * @return
  */
 @Override
 public ResponseResult getLinkById(Long id) {
  Link link = getById(id);
  LinkVo linkVo = BeanCopyUtils.copyBean(link, LinkVo.class);
  return ResponseResult.okResult(linkVo);
 }

 /**更新友链
  *
  * @param addLinkDto
  * @return
  */
 @Override
 public ResponseResult updateLink(AddLinkDto addLinkDto) {
  Link link = BeanCopyUtils.copyBean(addLinkDto, Link.class);
  link.setUpdateBy(SecurityUtils.getUserId());
  updateById(link);
  return ResponseResult.okResult();
 }

 /**删除友链
  *
  * @param ids
  * @return
  */
 @Override
 public ResponseResult deleteLink(List<Long> ids) {
  removeByIds(ids);
  return ResponseResult.okResult();
 }

 /**更新友链状态
  *
  * @param linkStatusDto
  * @return
  */
 @Override
 public ResponseResult updateLinkStatus(LinkStatusDto linkStatusDto) {
  Link link = BeanCopyUtils.copyBean(linkStatusDto, Link.class);
  link.setUpdateBy(SecurityUtils.getUserId());
  updateById(link);
  return ResponseResult.okResult();
 }


}
